package gamelogic

import (
	"server_cluster/common/config"
	"server_cluster/common/gdconf"
	"server_cluster/common/logger"
	"server_cluster/common/mysock"
	"server_cluster/common/protoc/pb"
	"server_cluster/logic/sharemem"
)

func CodexDataUpdateNotify(user *sharemem.User, codexType, codexId int32) {
	SendMsg(config.SC_CodexDataUpdateNotify, &pb.SC_CodexDataUpdateNotify{TypeDataList: []*pb.CodexTypeData{{
		Type: pb.CodexType(codexType),
		CodexList: []*pb.CodexData{{
			CodexId:    codexId,
			RewardTake: false,
		}},
	}}}, nil, user)
}

func UnlockCodex(_type pb.CodexType, id int32, user *sharemem.User) {
	var codexConfig *gdconf.CodexConfig = nil
	for _, codex := range gdconf.GetCodexConfigMap() {
		if codex.CodexType == int32(_type) && codex.OtherId == id {
			codexConfig = codex
		}
	}
	if codexConfig == nil {
		return
	}
	codex := user.GetCodex(codexConfig.CodexId)
	if codex == nil {
		codex = user.AddCodex(codexConfig.CodexId)
		if codex == nil {
			return
		}
		CodexDataUpdateNotify(user, codexConfig.CodexType, codexConfig.CodexId)
	}
	switch _type {
	case pb.CodexType_CODEX_ROLE:
	case pb.CodexType_CODEX_WEAPON:
	case pb.CodexType_CODEX_EQUIP:
	case pb.CodexType_CODEX_MONSTER:
	case pb.CodexType_CODEX_FISH:
		if user.GetLastFishLen() > codex.Param1 {
			codex.Param1 = user.GetLastFishLen()
		}
	default:
	}
}

//======消息协议处理=====

func CodexRewardTakeReq(user *sharemem.User, buf *mysock.SockBuf) int16 {
	req, ok := ParseMsg(config.CS_CodexRewardTakeReq, new(pb.CS_CodexRewardTakeReq), buf, user).(*pb.CS_CodexRewardTakeReq)
	if !ok {
		return config.PROTO_ERROR
	}

	codexIdList := make([]int32, 0)
	if req.Type == pb.CodexType_CODEX_NONE {
		codexIdList = append(codexIdList, req.CodexId)
	} else {
		for _, codexTypeData := range user.PacketCodexAllTypeData(nil) {
			if codexTypeData.Type != req.Type {
				continue
			}
			for _, codexData := range codexTypeData.CodexList {
				if codexData.RewardTake {
					continue
				}
				codexIdList = append(codexIdList, codexData.CodexId)
			}
		}
	}
	itemMap := make(map[int32]int32)
	for _, codexId := range codexIdList {
		codexConfig := gdconf.GetCodexConfig(codexId)
		if codexConfig == nil {
			continue
		}
		rewardItemId := codexConfig.RewardItem.ItemId
		rewardItemNum := codexConfig.RewardItem.ItemCount
		if rewardItemId == 0 {
			logger.Error("codex not have reward, codexId: %v, uid: %v", codexId, user.GetUid())
			continue
		}
		codex := user.GetCodex(codexId)
		if codex == nil {
			logger.Error("get codex is nil, codexId: %v", codexId)
			continue
		}
		if codex.RewardTake {
			logger.Error("codex reward already take, codexId: %v, uid: %v", codexId, user.GetUid())
			continue
		}
		codex.RewardTake = true
		itemMap[rewardItemId] += rewardItemNum
	}
	SendMsg(config.SC_CodexDataUpdateNotify, &pb.SC_CodexDataUpdateNotify{TypeDataList: user.PacketCodexAllTypeData(codexIdList)}, buf, user)
	AddItems(itemMap, user)
	pbItemList := make([]*pb.Item, 0)
	for id, num := range itemMap {
		pbItemList = append(pbItemList, &pb.Item{
			Id:  id,
			Num: num,
		})
	}

	return SendMsg(config.SC_CodexRewardTakeRsp, &pb.SC_CodexRewardTakeRsp{RetCode: config.OK, ItemList: pbItemList}, buf, user)
}

func GetCodexRoleFightPropReq(user *sharemem.User, buf *mysock.SockBuf) int16 {
	req, ok := ParseMsg(config.CS_GetCodexRoleFightPropReq, new(pb.CS_GetCodexRoleFightPropReq), buf, user).(*pb.CS_GetCodexRoleFightPropReq)
	if !ok {
		return config.PROTO_ERROR
	}
	_ = req

	pbRoleFightPropList := make([]*pb.CodexRoleFightProp, 0)
	for id := range gdconf.GetRoleConfigMap() {
		pbRoleFightPropList = append(pbRoleFightPropList, &pb.CodexRoleFightProp{
			RoleId:          id,
			MinFightPropMap: user.GetCodexRoleFightProp(id, false),
			MaxFightPropMap: user.GetCodexRoleFightProp(id, true),
		})
	}

	return SendMsg(config.SC_GetCodexRoleFightPropRsp, &pb.SC_GetCodexRoleFightPropRsp{RetCode: config.OK, RoleFightPropList: pbRoleFightPropList}, buf, user)
}
